using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Drawing;

namespace Map2.Xml
{
    class LoadXml
    {
        private List<Building> buildings = new List<Building>();
        private int mapWidth, mapHeight;
        private XmlTextReader xmlReader;

        public LoadXml(string file)
        {
            Building b = new Building();
            Point p = new Point(); ;

            xmlReader = new XmlTextReader(file);
            xmlReader.WhitespaceHandling = WhitespaceHandling.None;

            while (xmlReader.Read())
            {
                switch (xmlReader.LocalName)
                {
                    case "width":
                        mapWidth = int.Parse(xmlReader.ReadString());
                        break;
                    case "height":
                        mapHeight = int.Parse(xmlReader.ReadString());
                        break;
                    case "building":
                        if (!xmlReader.IsStartElement())
                        {
                            buildings.Add(b);
                            b = new Building();
                        }
                        break;
                    case "point":
                        p = new Point();
                        break;
                    case "x":
                        p.X = int.Parse(xmlReader.ReadString());
                        break;
                    case "y":
                        p.Y = int.Parse(xmlReader.ReadString());
                        b.addPoint(p);
                        break;
                }
            }
            xmlReader.Close();
        }

        public List<Building> getBuildings()
        {
            return buildings;
        }
        public Size getMapSize()
        {
            return new Size(mapWidth, mapHeight);
        }
    }
}
